//
//  TenderChujiaRVC.swift
//  ANOA
//
//  Created by 邱仙凯 on 2021/12/10.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit


class TenderChujiaRVC: TenderBaseTableController,UISearchBarDelegate {
    
    var page = 1
    
    var dataArray = [ZtbContentModel]()
    var z_zb_id = "" // 投标记录使用
    
    var btnflag = "1"
    var billTitle = ""
    
    var followTag = ""
    
    private var searchBar:UISearchBar?
    
    
    init(z_zb_id:String = "",searchTitle:String = "",followTag:String = "0") {
        super.init(nibName: nil, bundle: nil)
        self.followTag = followTag
        self.z_zb_id = z_zb_id
        self.helper.filterPa!.searchTitle = searchTitle
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "出价记录"
        let rightbtn = UIButton.creat4Item(image: UIImage(named: "all_record")!) { _ in
            getRecordListtt(zDtradeType: "",zb_id: self.z_zb_id, staff_id: "",billtitle: self.billTitle,zb_source: "0",s_data_source: "2")

        
        }
        let right = UIBarButtonItem(customView:rightbtn)
        self.navigationItem.rightBarButtonItem = right
        helper.callTableRefreshClosure = {
            self.tableView.mj_header?.beginRefreshing()
            
        }
        
        tableView.estimatedRowHeight = 50
        tableView.rowHeight = UITableView.automaticDimension
        tableView.backgroundColor = Theme.BACKGROUND_COLOR
        tableView.mj_header = KSRefreshHeader { [unowned self] in
            self.page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter { [unowned self] in
            self.page += 1
            self.network()
        }
        tableView.mj_header?.beginRefreshing()
        
        tableView.register(TenderChujiaRCell.self, forCellReuseIdentifier: TenderChujiaRCell.identifier)
       
        tableView.tableFooterView = UIView()
        
        
    }
    
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        self.helper.filterPa!.searchTitle = searchBar.text.unwrap
        self.tableView.mj_header?.beginRefreshing()
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows 
        return self.dataArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let one = self.dataArray[indexPath.row]
        
        guard let cell = tableView.dequeueReusableCell(withIdentifier: TenderChujiaRCell.identifier, for: indexPath) as? TenderChujiaRCell else {
            fatalError("cell error")
        }
        
        cell.makeConstrainsts()
        
        
        one.innerType = .chujiaRecordKHW
        
        cell.one = one
        cell.oper_btn.addTouchUpInSideAction {[one] _ in
            
            if one.zStatus.unwrap == "1" {
                let nav = BaseNavigationController()
                let vc = ZyaCarInfoVC()
                vc.dataArray = one.carInfo
                nav.viewControllers = [vc]
                self.present_FullScreen(to: nav)
            }
            
            
            
        }
        cell.mychujiaBtn.addTouchUpInSideAction { [one] _ in
             
            getRecordListtt(zDtradeType: one.zDtradeType.unwrap,zb_id: one.zZbId.unwrap, staff_id: one.zStaffId.unwrap,billtitle: self.billTitle,zb_source: "0",s_data_source: "2")
             
           
        }
        
        
        return cell
        
        
        
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
        self.helper.go2TenderTbDetailController(listType: .TB, controller:self,idkey:self.dataArray[indexPath.row].idKey.unwrap,innerType:.chujiaRecordKHW)
        
        
        
        
    }
    func network () {
        let  api = API.queryBidInfoList(page: "\(page)", size: "10", jp_id: z_zb_id, z_title: self.helper.filterPa!.searchTitle, z_sort: "1",s_data_source: "2")
 
        
        KSNetwork.shared.doRequest(api,showHud: false) { [weak self] (data:BaseResult<ZtbContentModel>) in
            guard let `self` = self else { return }
            if data.flag == TRUE {
                if let infos = data.infos {
                    if self.page == 1 {
                        self.dataArray.removeAll()
                    }
                    self.dataArray += infos
                    self.tableView.reloadData()
                    self.tableView.mj_header?.endRefreshing()
                    if infos.count < 10 {
                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    } else {
                        self.tableView.mj_footer?.endRefreshing()
                    }
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
        }
    }
    
    
    deinit {
        print("tenderfirst 销毁")
    }
}


func getRecordListtt(zDtradeType:String,zb_id:String,staff_id:String,billtitle:String,zb_source:String,s_data_source:String) {
    let api = API.tender(.queryBidRecordList(zb_id: zb_id, staff_id: staff_id,zb_source: zb_source,s_data_source: s_data_source,sUrid: USERINFO.idKey.unwrap))
     
     KSNetwork.shared.doRequest(api) { (data:BaseResult<ChujiaDetailResult>) in
         if data.flag == TRUE {
             if let infos = data.infoArrCombine {
                 let view = UIView()
                 view.backgroundColor = .white
                 view.layer.masksToBounds = true
                 view.layer.cornerRadius = 8
                 view.frame = CGRect(x:8,y:90,width:KSGlobal.screenW - 16,height:KSGlobal.screenH - 180)

                 let newsView = ChujiaDetailV()
                 newsView.zDtradeType = zDtradeType
                 newsView.titleLab.text = billtitle
                 newsView.dataArray = infos
                 newsView.reloadData()
                  view.addSubview(newsView)
                 newsView.snp.makeConstraints { make in
                     make.edges.equalTo(UIEdgeInsets(top: 5, left: 2, bottom: 30, right: 2))
                 }
         //                newsView.layer.masksToBounds = true
         //                newsView.layer.cornerRadius = 12
                  
                 
             
                     var v = UILabel()
                     v.text = "横线标识对应客户最后的出价"
                     v.textColor = .lightGray
                     v.font = UIFont.boldSystemFont(ofSize: 13)
                 view.addSubview(v)
                 v.snp.makeConstraints { make in
                     make.left.equalToSuperview().offset(5)
                     make.bottom.equalToSuperview().offset(-5)
                 }
                 
                 
                 
                 let topViewController = RouterUtils.currentTopViewController()!
                 topViewController.lew_presentPopupView( view, animation: LewPopupViewAnimationSpring()) {
                     print("动画结束")
                 }
             }
         } else {
             KSToast(data.errorCombine.unwrap)
         }
     }
}
